
package com.probiz.estoresa.system.web.action;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.aop.VerFuncInterceptor;
import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.system.SystemConfig;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.core.event.AdminCacheClearEvent;
import com.probiz.estore.core.event.ConfigChangedEvent;
import com.probiz.estore.core.event.EventUtil;
import com.probiz.estore.core.event.FrontCacheClearEvent;
import com.probiz.estore.system.service.SystemConfigManager;

public class SystemConfigController extends
		GenericStoreAdminController<SystemConfig> {
	private SystemConfigManager	mgr	= null;

	public void setSystemConfigManager(SystemConfigManager manager) {
		this.mgr = manager;
	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, String> configMap = mgr.getConfigMap();
		request.setAttribute("configMap", configMap);
		request.setAttribute("configUtil", ConfigUtil.getInstance());
		return new ModelAndView("system/systemConfigForm");
	}

	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) {
		String[] configKeys = request.getParameterValues("configKey");
		if (configKeys != null && configKeys.length > 0) {
			for (int i = 0; i < configKeys.length; i++) {
				String key = configKeys[i];
				String value = request.getParameter(key);

				SystemConfig systemConfig = mgr.getConfigByKey(key);
				if (value == null
						&& SystemConfig.CONF_DATA_TYPE_BOOLEAN
								.equals(systemConfig.getDataType())) {
					value = "false"; // 针对checkbox的处理
				}

				mgr.saveConfigByKey(key, value);
			}
			String message = getMessage("systemConfig.updated.succ",
					new Object[] { configKeys.length
							+ getMessage("common.items") });
			saveMessage(request, message);
		}

		EventUtil.getInstance().fire(new ConfigChangedEvent(this));
		ConfigUtil.getInstance().checkInvalidSystemConfigs();

		return defaultAction(request, response);
	}

	/**
	 * 清除后台缓存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView clearAdminSiteCache(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		EventUtil.getInstance().fire(new AdminCacheClearEvent(this));
		String message = getMessage("systemConfig.refreshAdminCache.succ");
		saveMessage(request, message);
		// 重载版本功能
		VerFuncInterceptor.getInstance().reload();
		return defaultAction(request, response);
	}
	/**
	 * 清除前台缓存
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 */
	public ModelAndView clearFrontSiteCache(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		EventUtil.getInstance().fire(new FrontCacheClearEvent(this));
		String message = getMessage("systemConfig.refreshFrontCache.succ");
		saveMessage(request, message);
		return defaultAction(request, response);
	}
	
	@Override
	protected String getEntityName(SystemConfig entity) {
		return entity.getConfigKey();
	}


	@Override
	protected void initController() throws Exception {

	}

	@Override
	protected void preSave(HttpServletRequest request, SystemConfig entity,
			BindException errors) {

	}
}
